package com.beaven.beavenime.activity.guide

import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.view.inputmethod.InputMethodManager
import com.beaven.beavenime.utils.isThisImeEnabled

class ImeEnableHandler(private val context: Context, private val im: InputMethodManager) : Handler() {

    companion object {
        const val CHECK_IME_ENABLE_WHAT = 0x001
        const val CHECK_IME_ENABLE_TIME = 300L
    }

    override fun handleMessage(msg: Message?) {
        super.handleMessage(msg)
        when (msg?.what) {
            CHECK_IME_ENABLE_WHAT -> {
                if (isThisImeEnabled(context, im)) {
                    startTopActivity()
                } else {
                    sendEmptyMessageDelayed(CHECK_IME_ENABLE_WHAT, CHECK_IME_ENABLE_TIME)
                }
            }
        }
    }

    private fun startTopActivity() {
        context.startActivity(Intent(context, GuideActivity::class.java).also {
            it.flags = (Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                    or Intent.FLAG_ACTIVITY_SINGLE_TOP
                    or Intent.FLAG_ACTIVITY_CLEAR_TOP)
        })
    }


}